﻿using PhotoKiosk.Properties;
using PhotoKiosk.ViewModel.Helpers;
using PhotoKiosk.ViewModel.UserControls;
using System.Resources;
using System.Windows;
using System.Windows.Input;

namespace PhotoKiosk.ViewModel
{
    public class ExitSystemViewModel : ViewModelBase
    {
        private DelegateCommand m_okCommand;
        private DelegateCommand m_cancelCommand;
        private DelegateCommand m_systemSettingsCommand;
        private MainWindowViewModel m_mainWindowViewModel;
        private NumbersPadAndTextViewModel m_numbersPadAndTextViewModel;
        private ViewModelBase m_lastViewModelBase;

        public ExitSystemViewModel(MainWindowViewModel i_mainWindowViewModel, ViewModelBase i_lastViewModelBase)
        {
            m_mainWindowViewModel = i_mainWindowViewModel;
            m_lastViewModelBase = i_lastViewModelBase;
        }

        public NumbersPadAndTextViewModel NumbersPadAndTextViewModel
        {
            get
            {
                if (m_numbersPadAndTextViewModel == null)
                {
                    m_numbersPadAndTextViewModel = new NumbersPadAndTextViewModel();
                }
                return m_numbersPadAndTextViewModel;
            }
        }

        public ICommand OkCommand
        {
            get
            {
                if (m_okCommand == null)
                {
                    m_okCommand = new DelegateCommand(param => this.okCommandExecute());
                }
                return m_okCommand;
            }

        }

        private void okCommandExecute()
        {
            if (checkSystemCode())
            {
                Application.Current.MainWindow.Close();
            }
        }

        public ICommand SystemSettingsCommand
        {
            get
            {
                if (m_systemSettingsCommand == null)
                {
                    m_systemSettingsCommand = new DelegateCommand(param => this.systemSettingsCommandExecute());
                }
                return m_systemSettingsCommand;
            }
        }

        private void systemSettingsCommandExecute()
        {
            if (checkSystemCode())
            {
                m_mainWindowViewModel.CurrentViewModel = new SystemSettingsViewModel(m_mainWindowViewModel);
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (m_cancelCommand == null)
                {
                    m_cancelCommand = new DelegateCommand(param => this.cancelCommandExecute());
                }
                return m_cancelCommand;
            }
        }

        private void cancelCommandExecute()
        {
            m_mainWindowViewModel.CurrentViewModel = m_lastViewModelBase;
        }

        private bool checkSystemCode()
        {
            bool result = true;
            if (!m_mainWindowViewModel.SystemData.SystemCloseCode.Equals(m_numbersPadAndTextViewModel.Number))
            {
                m_numbersPadAndTextViewModel.Error = m_mainWindowViewModel.ResourceManager.GetString("Text_Error_Wrong_Password");
                result = false;
            }
            return result;
        }
    }
}
